<?php
require_once './Base.php';


use PhpAmqpLib\Message\AMQPMessage;


class Push extends Base
{
    // 创建连接对象
    public function run()
    {
        $exchangeName = 'liu_qiang_test'; //交换机名
        $queueName = 'liu_qiang_test_queue'; //队列名称
        $routingKey = 'liu_qiang'; //路由关键字(也可以省略)
        //创建交换机   type类型 direct 可以不创建交换机使用默认也可以
        $this->exchangeDeclare($exchangeName, 'direct');
        // 创建队列
        $this->queueDeclare($queueName);
        //绑定队列在交换机上 必须绑定
        $this->queueBind($queueName, $exchangeName, $routingKey);
        for ($i = 0; $i <= 1000; $i++) {
            $msgBody = json_encode(["name" => "刘强" . mt_rand(), "age" => mt_rand()]);
            $msg = new AMQPMessage($msgBody, ['content_type' => 'text/plain', 'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT]); //生成消息
            $this->channel->basic_publish($msg, $exchangeName, $routingKey); //推送消息到某个交换机
        }
        $this->close();//关闭链接
    }
}

$obj = new push();
$obj->run();
